package com.base.key.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.key.domain.vo.TKeyHistoryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.key.domain.bo.TKeyHistoryBo;
import com.base.key.domain.TKeyHistory;
import com.base.key.mapper.TKeyHistoryMapper;
import com.base.key.service.ITKeyHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 密钥历史Service业务层处理
 *
 * @author base
 * @date 2023-10-17
 */
@RequiredArgsConstructor
@Service
public class TKeyHistoryServiceImpl implements ITKeyHistoryService {

    private final TKeyHistoryMapper baseMapper;

    /**
     * 查询密钥历史
     */
    @Override
    public TKeyHistoryVo queryById(String keyCode){
        return baseMapper.selectVoById(keyCode);
    }

    /**
     * 查询密钥历史列表
     */
    @Override
    public TableDataInfo<TKeyHistoryVo> queryPageList(TKeyHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TKeyHistory> lqw = buildQueryWrapper(bo);
        Page<TKeyHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询密钥历史列表
     */
    @Override
    public List<TKeyHistoryVo> queryList(TKeyHistoryBo bo) {
        LambdaQueryWrapper<TKeyHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TKeyHistory> buildQueryWrapper(TKeyHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TKeyHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getKeyType()), TKeyHistory::getKeyType, bo.getKeyType());
        lqw.eq(bo.getKeyLen() != null, TKeyHistory::getKeyLen, bo.getKeyLen());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), TKeyHistory::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyValue()), TKeyHistory::getKeyValue, bo.getKeyValue());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckStatus()), TKeyHistory::getCheckStatus, bo.getCheckStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TKeyHistory::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getUserCode()), TKeyHistory::getUserCode, bo.getUserCode());
        lqw.eq(StringUtils.isNotBlank(bo.getUserFlag()), TKeyHistory::getUserFlag, bo.getUserFlag());
        lqw.eq(bo.getKeyCreateTime() != null, TKeyHistory::getKeyCreateTime, bo.getKeyCreateTime());
        lqw.eq(bo.getKeyUpdateTime() != null, TKeyHistory::getKeyUpdateTime, bo.getKeyUpdateTime());
        lqw.eq(bo.getKeyStartTime() != null, TKeyHistory::getKeyStartTime, bo.getKeyStartTime());
        lqw.eq(bo.getKeyExpireTime() != null, TKeyHistory::getKeyExpireTime, bo.getKeyExpireTime());
        lqw.eq(bo.getKeyDestroyTime() != null, TKeyHistory::getKeyDestroyTime, bo.getKeyDestroyTime());
        lqw.eq(bo.getKeyArchiveTime() != null, TKeyHistory::getKeyArchiveTime, bo.getKeyArchiveTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDestroyCause()), TKeyHistory::getDestroyCause, bo.getDestroyCause());
        return lqw;
    }

    /**
     * 新增密钥历史
     */
    @Override
    public Boolean insertByBo(TKeyHistoryBo bo) {
        TKeyHistory add = BeanUtil.toBean(bo, TKeyHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setKeyCode(add.getKeyCode());
        }
        return flag;
    }

    /**
     * 修改密钥历史
     */
    @Override
    public Boolean updateByBo(TKeyHistoryBo bo) {
        TKeyHistory update = BeanUtil.toBean(bo, TKeyHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TKeyHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除密钥历史
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
